<?php

class Trang_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
		$this->_table = $this->config->item('database_tables');
	}

	// Public methods
	public function get_all_trang($limit,$start)
	{
		$this->db->select('*');
		$this->db->order_by('id', 'DESC');
		$this->db->limit($limit,$start);
		$query = $this->db->get('site_page');
		return $query->result_array();
	}
	
	public function count_all_trang()
	{
		$this->db->select('id');
		$this->db->from('site_page');
		return $this->db->count_all_results();
	}
	
	
	public function add_trang()	
	{
	
		$alias = auto_alias(strtolower($this->input->post('alias')));

		$data = array
					(
						'title' 		=> $this->input->post('title'),
						'alias' 		=> $alias,
						'description' 	=> $this->input->post('description'),
						'content'		=> html_entity_decode($this->input->post('content')),
						'creator_id'	=> $this->user->id,
						'status'		=> $this->input->post('status'),
					);
		$this->db->insert('site_page', $data);
	}
	
	public function change_status_trang($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_page',$data);		
	}	
	
	public function delete_trang($id)
	{
		$this->db->where('id', $id);
		$this->db->delete('site_page');
	}
	
	public function get_trang($id)
	{
		$this->db->select('*');
		$this->db->where('id',$id);
		$query = $this->db->get('site_page');
		return	$result = $query->row();
	}
	
	
	public function edit_trang($id)
	{
		if($this->input->post('alias')=='')
		{
			$alias = strtolower(auto_alias($this->input->post('title')));
		}
		else
		{
			$alias = auto_alias(strtolower($this->input->post('alias')));
		}
		
		$data = array(
			'title' 		=> $this->input->post('title'),
			'alias' 		=> $alias,
			'description' 	=> $this->input->post('description'),
			'content' 		=> html_entity_decode($this->input->post('content')),
			'creator_id'	=> $this->user->id,
			'status' 		=> $this->input->post('status'),
		);
		$this->db->where('id', $id);
		$this->db->update('site_page', $data);
	}
	
	public function check_alias()
	{
		$alias = auto_alias(strtolower($this->input->post('alias')));
        $query =  "(
			SELECT id, alias
			FROM site_content WHERE alias ='$alias' )
			UNION (			
			SELECT id, alias
			FROM site_category WHERE alias = '$alias')
			UNION(
			SELECT id, alias
			FROM site_page WHERE alias = '$alias')
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function search_data($keyword)
	{
		$this->db->select('*');
		$this->db->like('title', $keyword);
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get('site_page');
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}
}

/* End of file posts_model.php */
/* Location: ./application/modules/admin/models/posts_model.php */